class Solution {
public:
    int semiOrderedPermutation(vector<int>& nums) {
        int a1 = 0,an = 0;
        for(int i = 0;i < nums.size();i++){
            if(nums[i] == 1) a1 = i;
            if(nums[i] == nums.size()) an = i;
        }
        int ans = a1 + nums.size() - 1  - an;
        if(a1 > an) ans--;
        return ans;
    }
};